import {
	login,
	queryUserInfo
} from "@/api/login";

/**
 * 获取openid
 */
export const getToken = () => {
	//获取用户
	return new Promise((resolve, reject): void => {
		try {
			uni.login({
				success: async (loginRes) => {
					const res: any = await login({
						"code": loginRes.code,
					})
					uni.setStorageSync("token", res?.wx_token || "");
					resolve(res?.wx_token || "");
				}
			})
		} catch (e) {
			console.log(e)
			reject(e)
		}
	})
}

/**
 * 获取用户信息
 */
function getUserInfo(): Promise<any> {
	return new Promise(async (resolve, reject) => {
		try {
			const res = await queryUserInfo();
			if (res.code === 200) {
				uni.setStorageSync("userInfo", res.data);
				resolve(res.data);
			} else if (res.code === 500) {
				uni.setStorageSync('userInfo', null);
			}
		} catch (e) {
			console.log(e)
			reject(e)
		}
	})
}